﻿using System.IO;
using System.IO.Compression;


////////////////////////////////////////////////////////////////////////////////////////////////
//类名：Compressor
//说明：压缩类
//
//
//创建时间：2012-08-23
//版本：1.0.0.0
////////////////////////////////////////////////////////////////////////////////////////////////
public static class Compressor
{
    public static byte[] Compress(byte[] data)
    {
        MemoryStream memoryStream = new MemoryStream();
        GZipStream gzipStream = new GZipStream((Stream)memoryStream, CompressionMode.Compress, true);
        gzipStream.Write(data, 0, data.Length);
        gzipStream.Close();
        return memoryStream.ToArray();
    }

    public static byte[] Decompress(byte[] data)
    {
        MemoryStream memoryStream1 = new MemoryStream();
        memoryStream1.Write(data, 0, data.Length);
        memoryStream1.Position = 0;
        GZipStream gzipStream = new GZipStream((Stream)memoryStream1, CompressionMode.Decompress, true);
        MemoryStream memoryStream2 = new MemoryStream();
        byte[] buffer = new byte[64];
        for (int count = gzipStream.Read(buffer, 0, buffer.Length); count > 0; count = gzipStream.Read(buffer, 0, buffer.Length))
            memoryStream2.Write(buffer, 0, count);
        gzipStream.Close();
        return memoryStream2.ToArray();
    }
}
